package com.yjt.sousou.loading;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.permissionx.guolindev.PermissionX;
import com.yjt.sousou.R;
import com.yjt.sousou.base.BaseActivity;
import com.yjt.sousou.main.MainActivity;
import com.yjt.sousou.user.AccountManager;
import com.yjt.sousou.user.IUserChecker;
import com.yjt.sousou.user.LoginActivity;
import com.yjt.sousou.utils.StatusBarCompat;

import java.util.Timer;

import butterknife.BindView;

/**
 * 作者：chs
 * 时间：2018-10-22 14:29
 * 描述：欢迎页
 */
public class WelcomeActivity extends BaseActivity implements ITimerListener {

    @BindView(R.id.tv_version)
    TextView mTvVersion;
    private Timer mTimer = null;
    private int mCount = 1;

//    @OnClick(R.id.tv_launcher_timer)
//    void onClickTimerView() {
//        if (mTimer != null) {
//            mTimer.cancel();
//            mTimer = null;
//            checkIsShowScroll();
//        }
//    }
    @Override
    protected Object setContentLayout() {
        return R.layout.activity_loading;
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.white));
        StatusBarCompat.setLightMode(this);
        setVersion();
        mTimer = new Timer();
        final BaseTimerTask task = new BaseTimerTask(this);
        mTimer.schedule(task, 0, 500);
        getPermission();
    }

    private void getPermission() {
        PermissionX.init(this)
                .permissions(Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_PHONE_STATE)
                .onExplainRequestReason((scope, deniedList) -> ToastUtils.showLong("即将申请的权限是程序必须依赖的权限"))
                .onForwardToSettings((scope, deniedList) -> ToastUtils.showLong("您需要去应用程序设置当中手动开启权限"))
                .request((allGranted, grantedList, deniedList) -> {
                    if (allGranted) {
                    } else {
                        ToastUtils.showLong("有权限被禁止");
                    }
                });
    }

    private void setVersion() {
        String packetName = getPackageName();
        String versionCode = null;
        try {
            versionCode = getPackageManager().getPackageInfo(packetName, PackageManager.GET_CONFIGURATIONS).versionName;
            mTvVersion.setText("V" + versionCode);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    //判断是否显示滑动启动页
    private void checkIsShowScroll() {
            //检查用户是否登录了APP
            AccountManager.checkAccount(new IUserChecker() {
                @Override
                public void onSignIn() {
                    //去主页
                   startActivity(MainActivity.class);
                   finish();
                }

                @Override
                public void onNotSignIn() {
                  //去登录页
                    startActivity(LoginActivity.class);
                    finish();
                }
            });
    }
    @Override
    public void onTimer() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
//                if (mTvTimer != null) {
//                    mTvTimer.setText(MessageFormat.format("跳过\n{0}s", mCount));
                    mCount--;
                    if (mCount < 0) {
                        if (mTimer != null) {
                            mTimer.cancel();
                            mTimer = null;
                            checkIsShowScroll();
                        }
                    }
                }
//            }
        });
    }
}
